package com.cn.yang.dao.impl;

import com.cn.yang.dao.ContactUsDao;
import com.cn.yang.model.contactUs.ContactUsModel;
import com.cn.yang.model.message.MessageModel;
import com.cn.yang.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ContactUsDaoImpl implements ContactUsDao {
    JDBC jdbc = new JDBC();
    //查询总的
    @Override
    public ContactUsModel findContactUs(ContactUsModel model) {
        String sql = "select * from contactus limit "+model.getPageUtil().getPageNumber()+","+model.getPageUtil().getPageSize()+"";
        ResultSet resultSet = jdbc.getResultSet(sql);
        List<ContactUsModel> list = new ArrayList<>();
        ContactUsModel message = new ContactUsModel();
        try {
            while (resultSet.next()) {
                message.setId(resultSet.getInt("id"));
                message.setLocation(resultSet.getString("location"));
                message.setContactphone(resultSet.getString("contactphone"));
                message.setOrderphone(resultSet.getString("orderphone"));
                message.setEmail(resultSet.getString("email"));
                message.setNet(resultSet.getString("net"));
                message.setLine(resultSet.getString("line"));
                message.setWechat(resultSet.getString("wechat"));
                message.setTwitter(resultSet.getString("twitter"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        jdbc.closeConn();
        return message;
    }
    public ContactUsModel findContactUs() {
        String sql = "select * from contactus ";
        ResultSet resultSet = jdbc.getResultSet(sql);
        ContactUsModel message = new ContactUsModel();
        try {
            while (resultSet.next()) {
                
                message.setId(resultSet.getInt("id"));
                message.setLocation(resultSet.getString("location"));
                message.setContactphone(resultSet.getString("contactphone"));
                message.setOrderphone(resultSet.getString("orderphone"));
                message.setEmail(resultSet.getString("email"));
                message.setNet(resultSet.getString("net"));
                message.setLine(resultSet.getString("line"));
                message.setWechat(resultSet.getString("wechat"));
                message.setTwitter(resultSet.getString("twitter"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        jdbc.closeConn();
        return message;
    }
// 查询总的条数
    @Override
    public int countContactUs() {
        String sql = "select count(*) as count  from contactus ";
        ResultSet result = jdbc.getResultSet(sql);
        int count = 0;
        try{
            while (result.next()){
                count = result.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
// 查询 通过ID
    @Override
    public ContactUsModel selectById(Integer id) {
        String sql = "select * from contactus where id = "+id;
        ResultSet resultSet = jdbc.getResultSet(sql);
        ContactUsModel model = new ContactUsModel();
        try {
            while (resultSet.next()) {
                model.setId(resultSet.getInt("id"));
                model.setLocation(resultSet.getString("location"));
                model.setContactphone(resultSet.getString("contactphone"));
                model.setOrderphone(resultSet.getString("orderphone"));
                model.setEmail(resultSet.getString("email"));
                model.setNet(resultSet.getString("net"));
                model.setLine(resultSet.getString("line"));
                model.setWechat(resultSet.getString("wechat"));
                model.setTwitter(resultSet.getString("twitter"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        jdbc.closeConn();
        return model;
    }
//更新一条数据
    @Override
    public int updateContactUs(ContactUsModel model) {
        String sql = "update contactus set location = '"+model.getLocation()+"'," +
                "contactphone ='"+model.getContactphone()+"'," +
                "orderphone ='"+model.getOrderphone()+"'," +
                "email ='"+model.getEmail()+"'," +
                "net ='"+model.getNet()+"'," +
                "line ='"+model.getLine()+"'," +
                "wechat ='"+model.getWechat()+"'," +
                "twitter ='"+model.getTwitter()+"'";
        int result = jdbc.update(sql);
        jdbc.closeConn();
        return result;
    }
}
